package 题集.牛客.面试高频榜;

import java.util.ArrayList;
import java.util.List;


class ListNode {
    int val;
    ListNode next = null;

    public ListNode(int val) {
        this.val = val;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }
}

public class Solution {
    /**
     * 判断链表是否为回文结构
     *
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    public static boolean isPail(ListNode head) {
        if (head == null) {
            return false;
        }

        List<Integer> list = new ArrayList<>();
        // 遍历链表，生成字符串
        while (head != null) {
            list.add(head.val);
            if (head.next == null) {
                break;
            }
            head = head.next;
        }

        int length = list.size();
        //判断是否回文
        for (int i = 0, j = length - 1; i <= j; i++, j--) {
            if (list.get(i).intValue() != list.get(j).intValue()) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        //-401261,-449050,-456674,-456674,-449050,-401261
        ListNode head1 = new ListNode(-401261);
        ListNode head2 = new ListNode(-449050);
        ListNode head3 = new ListNode(-456674);
        ListNode head4 = new ListNode(-456674);
        ListNode head5 = new ListNode(-449050);
        ListNode head6 = new ListNode(-401261);

        head1.setNext(head2);
        head2.setNext(head3);
        head3.setNext(head4);
        head4.setNext(head5);
        head5.setNext(head6);
        System.out.println(isPail(head1));
    }
}